#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/11/2 10:30
# @Author  : Andrewq
# @Site    : nope
# @File    : noteResource.py
# @Software: PyCharm

from flask import request, jsonify
from flask_restplus import Resource
from . import main_rest_api, back_model

from ..service.userCollectManager import UserCollectManager
from ..utils.response_code import RET

note_get_parser = main_rest_api.parser()
note_get_parser.add_argument('collectID', type=str, location='args', required=True)
note_get_parser.add_argument('type', type=str, location='args', required=True)

note_post_parser = main_rest_api.parser()
note_post_parser.add_argument('parentID', type=str, location='form', required=True)
note_post_parser.add_argument('type', type=str, location='form', required=True)

note_delete_parser = main_rest_api.parser()
note_delete_parser.add_argument('collectID', type=str, location='form', required=True)

note_update_parser = main_rest_api.parser()
note_update_parser.add_argument('collectID', type=str, location='form', required=True)
note_update_parser.add_argument('text', type=str, location='form', required=True)

note_folder_delete_parser = main_rest_api.parser()
note_folder_delete_parser.add_argument('collectID', type=str, location='form', required=True)

note_list_get_parser = main_rest_api.parser()

note_other_get_parser = main_rest_api.parser()
note_other_get_parser.add_argument('collectID', type=str, location='args', required=True)

note_other_rename_parser = main_rest_api.parser()
note_other_rename_parser.add_argument('title', type=str, location='form', required=True)
note_other_rename_parser.add_argument('collectID', type=str, location='form', required=True)
note_other_rename_parser.add_argument('type', type=str, location='form', required=True)


class NoteResource(Resource):

    @main_rest_api.doc(description="获取单个笔记")
    @main_rest_api.marshal_with(back_model, skip_none=True)
    @main_rest_api.expect(note_get_parser)
    def get(self):
        kwargs = note_get_parser.parse_args()

        result_dict = UserCollectManager.get_note(**{'collect_id': kwargs.get('collectID'),
                                                     'collect_type': kwargs.get('type')})

        return result_dict

    @main_rest_api.doc(description="添加笔记")
    @main_rest_api.marshal_with(back_model, skip_none=True)
    @main_rest_api.expect(note_post_parser)
    def post(self):
        kwargs = note_post_parser.parse_args()

        args = {
            'parentID': kwargs.get('parentID'),
            'type': kwargs.get('type'),
            'user_id': kwargs.get('userID')
        }

        result_dict = UserCollectManager.add_note(**args)

        return result_dict

    @main_rest_api.doc(description="删除笔记")
    @main_rest_api.marshal_with(back_model, skip_none=True)
    @main_rest_api.expect(note_delete_parser)
    def delete(self):
        kwargs = note_delete_parser.parse_args()

        args = {
            'collect_id': kwargs.get('collectID')
        }

        result_dict = UserCollectManager.delete_note(**args)

        return result_dict

    @main_rest_api.doc(description="更新笔记")
    @main_rest_api.marshal_with(back_model, skip_none=True)
    @main_rest_api.expect(note_update_parser)
    def put(self):
        kwargs = note_update_parser.parse_args()

        args = {
            'collect_id': kwargs.get('collectID'),
            'text': kwargs.get('text')
        }

        result_dict = UserCollectManager.update_note(**args)

        return result_dict


class NoteFolderResource(Resource):

    @main_rest_api.doc(description="删除笔记文件夹")
    @main_rest_api.marshal_with(back_model, skip_none=True)
    @main_rest_api.expect(note_folder_delete_parser)
    def delete(self):
        kwargs = note_folder_delete_parser.parse_args()

        args = {
            'collect_id': kwargs.get('collectID')
        }

        result_dict = UserCollectManager.delete_note_folder(**args)

        return result_dict


class NoteListResource(Resource):

    @main_rest_api.doc(description="获取笔记列表")
    @main_rest_api.marshal_with(back_model, skip_none=True)
    @main_rest_api.expect(note_list_get_parser)
    def get(self):
        kwargs = note_list_get_parser.parse_args()

        result_dict = UserCollectManager.get_note_list(**{})

        return result_dict


class NoteOtherResource(Resource):

    @main_rest_api.doc(description="获取所有父节点collectID")
    @main_rest_api.marshal_with(back_model, skip_none=True)
    @main_rest_api.expect(note_other_get_parser)
    def get(self):
        kwargs = note_other_get_parser.parse_args()

        result_dict = UserCollectManager.get_all_parent_ID(**kwargs)

        return result_dict

    @main_rest_api.doc(description="重命名文件")
    @main_rest_api.marshal_with(back_model, skip_none=True)
    @main_rest_api.expect(note_other_rename_parser)
    def put(self):
        kwargs = note_other_rename_parser.parse_args()

        args = {
            'collect_id': kwargs.get('collectID'),
            'collect_type': kwargs.get('collect_type'),
            'title': kwargs.get('title')
        }

        result_dict = UserCollectManager.rename_note(**args)

        return result_dict
